முடிவை வரம்பிடுதல்
வினாவிலிருந்து திருப்பி அனுப்பப்படும் பதிவுகளின் எண்ணிக்கையை "LIMIT" அறிக்கையைப் பயன்படுத்தி வரம்பிடலாம்:
எடுத்துக்காட்டு
"customers" அட்டவணையில் முதல் 5 பதிவுகளைத் தேர்ந்தெடுக்கவும்:
let mysql = require('mysql');
let con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
let sql = "SELECT * FROM customers LIMIT 5";
con.query(sql, function (err, result) {
if (err) throw err;
console.log(result);
});
});
மேலே உள்ள குறியீட்டை "demo_db_limit.js" என்ற கோப்பில் சேமித்து கோப்பை இயக்கவும்:
C:\Users\Your Name>node demo_db_limit.js
இது உங்களுக்கு இந்த முடிவைக் கொடுக்கும்:
[
{ id: 1, name: 'John', address: 'Highway 71'},
{ id: 2, name: 'Peter', address: 'Lowstreet 4'},
{ id: 3, name: 'Amy', address: 'Apple st 652'},
{ id: 4, name: 'Hannah', address: 'Mountain 21'},
{ id: 5, name: 'Michael', address: 'Valley 345'}
]
LIMIT முக்கிய குறிப்புகள்:
- செயல்திறன்: பெரிய அட்டவணைகளில் செயல்திறனை மேம்படுத்துகிறது
- நினைவகம்: நினைவக பயன்பாட்டைக் குறைக்கிறது
- பக்கமிடல்: வலை பக்கமிடலுக்கு அத்தியாவசியமானது
- காட்சிப்படுத்தல்: பயனர் இடைமுகத்தில் காட்டப்படும் தரவைக் கட்டுப்படுத்துகிறது
வேறு நிலையில் இருந்து தொடங்கவும்
மூன்றாவது பதிவில் இருந்து தொடங்கி, அடுத்த ஐந்து பதிவுகளைத் திருப்பி அனுப்ப விரும்பினால், "OFFSET" முக்கியச்சொல்லைப் பயன்படுத்தலாம்:
எடுத்துக்காட்டு
நிலை 3 இல் இருந்து தொடங்கி, அடுத்த 5 பதிவுகளைத் திருப்பி அனுப்பவும்:
let mysql = require('mysql');
let con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
let sql = "SELECT * FROM customers LIMIT 5 OFFSET 2";
con.query(sql, function (err, result) {
if (err) throw err;
console.log(result);
});
});
குறிப்பு: "OFFSET 2" என்பது இரண்டாவது நிலையில் இருந்து அல்ல, மூன்றாவது நிலையில் இருந்து தொடங்குவதாகும்!
மேலே உள்ள குறியீட்டை "demo_db_offset.js" என்ற கோப்பில் சேமித்து கோப்பை இயக்கவும்:
C:\Users\Your Name>node demo_db_offset.js
இது உங்களுக்கு இந்த முடிவைக் கொடுக்கும்:
[
{ id: 3, name: 'Amy', address: 'Apple st 652'},
{ id: 4, name: 'Hannah', address: 'Mountain 21'},
{ id: 5, name: 'Michael', address: 'Valley 345'},
{ id: 6, name: 'Sandy', address: 'Ocean blvd 2'},
{ id: 7, name: 'Betty', address: 'Green Grass 1'}
]
OFFSET செயல்பாடு
- பூஜ்ஜிய-அடிப்படை: OFFSET 0 முதல் பதிவில் இருந்து தொடங்குகிறது
- தவிர்க்கும்: OFFSET N முதல் N பதிவுகளைத் தவிர்க்கிறது
- பக்கமிடல்: பக்கமிடல் செயல்பாடுகளுக்கு பயனுள்ளதாக இருக்கிறது
- செயல்திறன்: பெரிய OFFSET மதிப்புகள் செயல்திறனைக் குறைக்கலாம்
குறுகிய தொடரியல்
உங்கள் SQL அறிக்கையை "LIMIT 2, 5" போன்று எழுதலாம், இது மேலே உள்ள offset எடுத்துக்காட்டின் அதே முடிவைத் தரும்:
எடுத்துக்காட்டு
நிலை 3 இல் இருந்து தொடங்கி, அடுத்த 5 பதிவுகளைத் திருப்பி அனுப்பவும்:
let mysql = require('mysql');
let con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
let sql = "SELECT * FROM customers LIMIT 2, 5";
con.query(sql, function (err, result) {
if (err) throw err;
console.log(result);
});
});
குறிப்பு: எண்கள் தலைகீழாக உள்ளன: "LIMIT 2, 5" என்பது "LIMIT 5 OFFSET 2" போன்றதே
| தொடரியல் | விளக்கம் | முடிவு |
|---|---|---|
| LIMIT 5 | முதல் 5 பதிவுகள் | பதிவுகள் 1-5 |
| LIMIT 5 OFFSET 2 | முதல் 2 பதிவுகளைத் தவிர்த்து அடுத்த 5 பதிவுகள் | பதிவுகள் 3-7 |
| LIMIT 2, 5 | முதல் 2 பதிவுகளைத் தவிர்த்து அடுத்த 5 பதிவுகள் | பதிவுகள் 3-7 |
| LIMIT 0, 10 | முதல் 10 பதிவுகள் | பதிவுகள் 1-10 |
| LIMIT 10, 10 | முதல் 10 பதிவுகளைத் தவிர்த்து அடுத்த 10 பதிவுகள் | பதிவுகள் 11-20 |
மேம்பட்ட LIMIT நுட்பங்கள்
ORDER BY உடன் LIMIT
// Get top 5 customers by name
con.query("SELECT * FROM customers ORDER BY name ASC LIMIT 5", function (err, result) {
if (err) throw err;
console.log("Top 5 customers by name:", result);
});
// Get most recent 10 orders
con.query("SELECT * FROM orders ORDER BY order_date DESC LIMIT 10", function (err, result) {
if (err) throw err;
console.log("Most recent 10 orders:", result);
});
WHERE உடன் LIMIT
// Get first 3 active customers
con.query("SELECT * FROM customers WHERE status = 'active' LIMIT 3", function (err, result) {
if (err) throw err;
console.log("First 3 active customers:", result);
});
// Get limited results with complex conditions
con.query("SELECT * FROM products WHERE price > 100 AND category = 'electronics' LIMIT 5", function (err, result) {
if (err) throw err;
console.log("Expensive electronics (limited to 5):", result);
});
மாறிகள் மூலம் பக்கமிடல்
// Pagination with variables
const page = 2;
const pageSize = 10;
const offset = (page - 1) * pageSize;
let sql = "SELECT * FROM customers LIMIT ? OFFSET ?";
con.query(sql, [pageSize, offset], function (err, result) {
if (err) throw err;
console.log(`Page ${page} results:`, result);
});
// Dynamic pagination function
function getPaginatedResults(page, pageSize) {
const offset = (page - 1) * pageSize;
let sql = "SELECT * FROM customers ORDER BY id LIMIT ? OFFSET ?";
con.query(sql, [pageSize, offset], function (err, result) {
if (err) throw err;
console.log(`Page ${page} (${pageSize} items):`, result);
});
}
// Usage
getPaginatedResults(1, 5); // Page 1, 5 items
getPaginatedResults(2, 5); // Page 2, 5 items
வெவ்வேறு சூழ்நிலைகளில் LIMIT
முதல் N பதிவுகள்
// Get first record only
con.query("SELECT * FROM customers LIMIT 1", function (err, result) {
if (err) throw err;
console.log("First customer:", result[0]);
});
// Get last 5 records (assuming ordered by ID)
con.query("SELECT * FROM customers ORDER BY id DESC LIMIT 5", function (err, result) {
if (err) throw err;
console.log("Last 5 customers:", result);
});
சீரற்ற மாதிரிகள்
// Get random 3 records
con.query("SELECT * FROM customers ORDER BY RAND() LIMIT 3", function (err, result) {
if (err) throw err;
console.log("Random 3 customers:", result);
});
// Get sample data for testing
con.query("SELECT * FROM products ORDER BY RAND() LIMIT 10", function (err, result) {
if (err) throw err;
console.log("Random product sample:", result);
});
குழுவாக்கத்துடன் LIMIT
// Get top product from each category
con.query(`
SELECT * FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY category ORDER BY price DESC) as rank
FROM products
) ranked
WHERE rank = 1
`, function (err, result) {
if (err) throw err;
console.log("Top product from each category:", result);
});
LIMIT சிறந்த நடைமுறைகள்
செயல்திறன்
- பெரிய முடிவு செட்களுக்கு எப்போதும் LIMIT பயன்படுத்தவும்
- ORDER BY உடன் குறியீட்டு நெடுவரிசைகளைப் பயன்படுத்தவும்
- பெரிய OFFSET மதிப்புகளைத் தவிர்க்கவும்
- பக்கமிடலுக்கு WHERE உடன் கூடிய குறியீட்டு நெடுவரிசைகளைப் பயன்படுத்தவும்
பயனர் அனுபவம்
- பக்கத்திற்கு 10-50 பதிவுகளைக் காட்டவும்
- மொத்த எண்ணிக்கையைக் காட்டவும் (பக்கமிடலுக்கு)
- அதிகபட்ச வரம்பை அமைக்கவும்
- பயனர் தேர்வு வரம்புகளை அனுமதிக்கவும்
குறியீடு தரம்
- மாறிகள் மூலம் வரம்புகளை மாற்றக்கூடியதாக்கவும்
- பக்கமிடல் செயல்பாடுகளை மறுபயன்பாட்டிற்காக உருவாக்கவும்
- வரம்பு மதிப்புகளை உள்ளீட்டிலிருந்து சரிபார்க்கவும்
- வரம்புகள் இல்லாத வினாக்களுக்கு கவனமாக இருங்கள்
முழுமையான எடுத்துக்காட்டு
அனைத்து LIMIT நுட்பங்களையும் உள்ளடக்கிய முழுமையான எடுத்துக்காட்டு:
const mysql = require('mysql');
// Create connection
const con = mysql.createConnection({
host: "localhost",
user: "root",
password: "password",
database: "company_db"
});
// Connect to MySQL
con.connect((err) => {
if (err) {
console.error('Error connecting to MySQL:', err.message);
return;
}
console.log('Connected to MySQL database');
// Example 1: Basic LIMIT
console.log('\n1. First 5 customers:');
con.query("SELECT * FROM customers LIMIT 5", (err, result) => {
if (err) {
console.error('Error in query 1:', err.message);
return;
}
console.log(result);
});
// Example 2: LIMIT with OFFSET
console.log('\n2. Customers with OFFSET (records 3-7):');
con.query("SELECT * FROM customers LIMIT 5 OFFSET 2", (err, result) => {
if (err) {
console.error('Error in query 2:', err.message);
return;
}
console.log(result);
});
// Example 3: Shorter syntax LIMIT
console.log('\n3. Customers with shorter syntax (records 3-7):');
con.query("SELECT * FROM customers LIMIT 2, 5", (err, result) => {
if (err) {
console.error('Error in query 3:', err.message);
return;
}
console.log(result);
});
// Example 4: Pagination system
console.log('\n4. Pagination example:');
function getCustomersPage(page, pageSize) {
const offset = (page - 1) * pageSize;
const sql = "SELECT * FROM customers ORDER BY id LIMIT ? OFFSET ?";
con.query(sql, [pageSize, offset], (err, result) => {
if (err) {
console.error('Error in pagination:', err.message);
return;
}
console.log(`Page ${page} (items ${offset + 1}-${offset + result.length}):`, result);
});
}
// Get multiple pages
getCustomersPage(1, 3); // Page 1: records 1-3
getCustomersPage(2, 3); // Page 2: records 4-6
// Example 5: Get total count for pagination
console.log('\n5. Pagination with total count:');
con.query("SELECT COUNT(*) as total FROM customers", (err, countResult) => {
if (err) {
console.error('Error getting count:', err.message);
return;
}
const totalCustomers = countResult[0].total;
const pageSize = 5;
const totalPages = Math.ceil(totalCustomers / pageSize);
console.log(`Total customers: ${totalCustomers}`);
console.log(`Page size: ${pageSize}`);
console.log(`Total pages: ${totalPages}`);
// Close connection
setTimeout(() => {
con.end((err) => {
if (err) {
console.error('Error closing connection:', err.message);
return;
}
console.log('\nConnection closed');
});
}, 2000);
});
});